<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('announcement');
	}
	
	public function index($offset = '0') {
		$this->_get_userinfo();
		
		$this->load->library('pagination');
		$config['base_url'] = site_url('mahalum/home/index');
		$config['total_rows'] = $this->announcement->count_announcement();
		$config['per_page'] = 1; 
		$config['uri_segment'] = 4;
		$config['query_string_segment'] = 'page';
		$config['display_pages'] = FALSE;
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$offset = $this->_validate_offset($offset, $config['total_rows'], 'mahalum/home');
		$this->pagination->initialize($config);
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'home'));
		$params = array(
			'announcements' => $this->announcement->get_announcement($offset, 1),
			'paging' => $this->pagination->create_links(),
			'addstatus' => $this->session->flashdata('addstatus'),
			'editstatus' => $this->session->flashdata('editstatus')
		);
		$this->load->view('common/announcement_view', $params);
		$this->_load_common_footer();
	}
	
	public function lists() {
		$this->_get_userinfo();
		
		$this->load->helper('table');
		
		$limit = $this->announcement->count_announcement();
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'home'));
		$params = array(
			'announcements' => $this->announcement->get_announcement('0', $limit),
			'deletestatus' => $this->session->flashdata('deletestatus'),
			'editrole' => $this->session->flashdata('editrole'),
			'deleterole' => $this->session->flashdata('deleterole'),
			'role' => $this->userinfo['role'],
			'pager' => get_pager(4)
		);
		$this->load->view('common/announcement_creator_list', $params);
		$this->_load_common_footer();
	}
	
	public function delete($id = NULL) {
		$this->_get_userinfo();
		
		if ($id == NULL) {
			redirect('mahalum/home/lists', 'location');
			exit;
		}
		$announcement = $this->announcement->get_announcement_by_id($id);
		if ($announcement == NULL) {
			redirect('mahalum/home/lists', 'refresh');
			exit;
		}
		if ($this->userinfo['username'] != $announcement->creator) {
			$this->session->set_flashdata('deleterole', 'failed');
			redirect('mahalum/home/lists', 'refresh');
			exit;
		}
		
		$status = $this->input->post('confirm');
		if ($status === FALSE) {
			$this->_load_common_header();
			$this->_load_common_navigation();
			$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'home'));
			$params = array(
				'announcement' => $announcement,
				'role' => $this->userinfo['role']
			);
			$this->load->view('common/announcement_confirm', $params);
			$this->_load_common_footer();
		} else {
			if ($status == 'Proses') {
				$id = $this->input->post('id');
				$result = $this->announcement->delete_announcement($id);
				if ($result === FALSE) {
					$this->session->set_flashdata('deletestatus', 'failed');	
				} else {
					$this->session->set_flashdata('deletestatus', 'success');
				}
				redirect('mahalum/home/lists', 'refresh');
			} else {
				redirect('mahalum/home/lists', 'refresh');
			}
		}
	}
	
	public function edit($id = NULL) {
		$this->_get_userinfo();
		
		if ($id == NULL) {
			redirect('mahalum/home/lists', 'location');
			exit;
		}
		$announcement = $this->announcement->get_announcement_by_id($id);
		if ($announcement == NULL) {
			redirect('mahalum/home/lists', 'refresh');
			exit;
		}
		if ($this->userinfo['username'] != $announcement->creator) {
			$this->session->set_flashdata('editrole', 'failed');
			redirect('mahalum/home/lists', 'refresh');
			exit;
		}
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		$status = $this->input->post('confirm');
		if ($this->form_validation->run('add_announcement') == FALSE) {
			if ($status == 'Batal') {
				redirect('mahalum/home/index', 'location');
			} else {
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'home'));
				$this->load->view('common/tinymce');
				$params = array(
					'announcement' => $announcement,
					'editstatus' => $this->session->flashdata('editstatus'),
					'title' => $this->session->flashdata('title'),
					'content' => $this->session->flashdata('content'),
					'role' => $this->userinfo['role']
				);
				$this->load->view('common/announcement_editform', $params);
				$this->_load_common_footer();
			}
		} else {
			if ($status == 'Post') {
				$title = $this->input->post('title');
				$content = $this->input->post('content');
				$result = $this->announcement->edit_announcement($id, $this->userinfo['username'], $title, $content);
				if ($result == FALSE) {
					$this->session->set_flashdata('editstatus', 'failed');
					$this->session->set_flashdata('title', $title);
					$this->session->set_flashdata('content', $content);
					redirect('mahalum/home/edit/'.$id, 'refresh');
				} else {
					$this->session->set_flashdata('editstatus', 'success');
					redirect('mahalum/home/index', 'refresh');
				}
			} else {
				redirect('mahalum/home/lists', 'refresh');
			}
		}
	}
	
	public function add() {
		$this->_get_userinfo();
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		$status = $this->input->post('confirm');
		if ($this->form_validation->run('add_announcement') == FALSE) {
			if ($status == 'Batal') {
				redirect('mahalum/home/index', 'location');
			} else {
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'home'));
				$this->load->view('common/tinymce');
				$params = array(
					'addstatus' => $this->session->flashdata('addstatus'),
					'title' => $this->session->flashdata('title'),
					'content' => $this->session->flashdata('content'),
					'role' => $this->userinfo['role']
				);
				$this->load->view('common/announcement_form', $params);
				$this->_load_common_footer();
			}
		} else {
			if ($status == 'Post') {
				$title = $this->input->post('title');
				$content = $this->input->post('content');
				$result = $this->announcement->add_announcement($this->userinfo['username'], $title, $content);
				if ($result == FALSE) {
					$this->session->set_flashdata('addstatus', 'failed');
					$this->session->set_flashdata('title', $title);
					$this->session->set_flashdata('content', $content);
					redirect('mahalum/home/add', 'refresh');
				} else {
					$this->session->set_flashdata('addstatus', 'success');
					redirect('mahalum/home/index', 'refresh');
				}
			} else {
				redirect('mahalum/home/index', 'refresh');
			}
		}
	}
}